package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.User;
import com.itheima.domain.UserInfo;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.mongo.Friend;
import com.itheima.mongo.UserLike;
import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户基础信息的dubbo实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据id查询
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    //更新
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //保存
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //根据推荐用户id集合和条件查询用户信息的方法
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id",userIds);// id in (1,2,3,4,5)
        //2、添加筛选条件---》
        if(info != null) {
            if(info.getAge() != null) {
                qw.lt("age",info.getAge()); // age < 30
            }
            if(!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender",info.getGender()); // gender = man
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    //更新用户头像
    @Override
    public void uss(Long userId, String videoUrl) {
        UserInfo userInfo=new UserInfo();
        userInfo.setId(userId);
        userInfo.setAvatar(videoUrl);
        userInfoMapper.updateById(userInfo);
    }


    //查询所有用户信息方法
    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<UserInfo>(page,pagesize),null);
    }

    //统计喜欢 喜欢我 相互喜欢人数
    @Override
    public Map tongji(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        Long wo= mongoTemplate.count(query, Friend.class);

        Query query1=Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Long ni=mongoTemplate.count(query1,UserLike.class);

        Query query2=Query.query(Criteria.where("likeUser").is(userId).and("isLike").is(true));
        Long he=mongoTemplate.count(query2,UserLike.class);

        Map map=new HashMap();
        map.put("userId",wo);
        map.put("likeUserId",ni);
        map.put("isLike",he);

        return map;
    }


}
